﻿using System;
using System.Runtime.Serialization;
using System.Threading;
using Peak.Core;

namespace Peak.Web.Controllers.Models
{
  public class TaskFilterModel 
  {
    public bool StatusToDo { get; set; }
    public bool StatusInProgress { get; set; }
    public bool StatusReadyForTest { get; set; }
    public bool StatusDone { get; set; }
    public string QuickFilter { get; set; }
    public SortDirection SortDirection { get; set; }
    public TaskSortOrder SortOrder { get; set; }
    public string AssignedTo { get; set; }

    public FilterTaskStatus Status
    {
      get
      {
        FilterTaskStatus result = new FilterTaskStatus();
        if (StatusToDo)
          result |= FilterTaskStatus.ToDo;
        if (StatusInProgress)
          result |= FilterTaskStatus.InProgress;
        if (StatusReadyForTest)
          result |= FilterTaskStatus.ReadyForTest;
        if (StatusDone)
          result |= FilterTaskStatus.Done;

        return result;
      }
    }
    
    
    public SortDirection InverseSortDirection(TaskSortOrder sortOrder)
    {
      if(SortOrder != sortOrder)
        return Core.SortDirection.Asc;
      if (SortDirection == Core.SortDirection.Asc)
        return Core.SortDirection.Desc;
      else
        return Core.SortDirection.Asc;
    }

    public TaskFilterModel()
    {
      StatusInProgress = StatusToDo = StatusReadyForTest = true;
      StatusDone = false;      
    }

    public void Update(TaskFilter taskFilter, int projectId)
    {      
      taskFilter.QuickFilter = QuickFilter;
      taskFilter.SortDirection = SortDirection;
      taskFilter.SortOrder = SortOrder;
      if (StatusToDo)
        taskFilter.Status |= FilterTaskStatus.ToDo;
      if (StatusInProgress)
        taskFilter.Status |= FilterTaskStatus.InProgress;
      if (StatusReadyForTest)
        taskFilter.Status |= FilterTaskStatus.ReadyForTest;
      if (StatusDone)
        taskFilter.Status |= FilterTaskStatus.Done;
    }

    public string GetDescription()
    {
      string result = "";
      if(StatusToDo && StatusInProgress && StatusDone)
      {
        result = "All tasks";
      }
      else
      {
        result = "tasks in status [";
        if(StatusToDo)
        {
          result += "To do,";
        }
        if(StatusInProgress)
        {
          result += "in progress,";
        }        
        if(StatusDone)
        {
          result += "done,";
        }
        result = result.Remove(result.Length - 1, 1);
        result += "]";      
      }
      if (AssignedTo != null)
      {
        if (AssignedTo == Thread.CurrentPrincipal.Identity.Name)
        {
          result += ", assigned to me";
        }
        else
        {
          result += ", assigned to user [" + AssignedTo + "]";
        }
      }
      return result;
    }

    public TaskFilterModel GetFilter(TaskSortOrder order)
    {
      TaskFilterModel taskFilterModel = new TaskFilterModel();
      taskFilterModel.QuickFilter = QuickFilter;
      if(SortDirection == Core.SortDirection.Asc)
        taskFilterModel.SortDirection = Core.SortDirection.Desc;
      else
        taskFilterModel.SortDirection = Core.SortDirection.Asc;
      taskFilterModel.SortOrder = order;
      taskFilterModel.StatusDone = StatusDone;
      taskFilterModel.StatusInProgress = StatusInProgress;
      taskFilterModel.StatusToDo = StatusToDo;
      return taskFilterModel;
    }
  }
}